#include "Framework.h"
#include "display/state/StateManager.h"
#include "display/dialog/DialogManager.h"
#include "display/dialog/Dialog.h"
#include "utils/CoordinateUtil.h"
#include "display/dialog/ModelDialog.h"

Framework::Framework(void)
	:curState(NULL), mLastState(""),mCurState("")
{
	mainScene = CCScene::create();
	stateLayer = CCLayer::create();
	globalLayer = CCLayer::create();
	modelDialogLayer = CCLayer::create();
	modelLessDialogLayer = CCLayer::create();
	maskLayer = CCNode::create();
	topLayer = CCLayer::create();
	mainScene->addChild(stateLayer);
	mainScene->addChild(globalLayer);
	mainScene->addChild(modelDialogLayer);
	mainScene->addChild(modelLessDialogLayer);
	mainScene->addChild(topLayer);
}


Framework::~Framework(void)
{
}

void Framework::init()
{
	CCSprite* pSprite = CCSprite::create("sprite/mask.png");
	pSprite->setPosition(cult(pSprite, 0, -160));
	maskLayer->addChild(pSprite);

	ModelDialog::masks = Mask::create(100);

	pDirector = CCDirector::sharedDirector();
	pDirector->runWithScene(mainScene);
}

void Framework::changeState( const char* name )
{
	assert(stateManager != NULL);
	mLastState = mCurState;
	mCurState = name;
	State* state = stateManager->getState(name);
	changeState(state);

	/*State* state = StateManager::getSingletonPtr()->getState(name);
	CCScene* pScene = CCScene::create();
	pScene->addChild(state);
	CCDirector::sharedDirector()->runWithScene(pScene);*/
}

void Framework::changeState( State* state )
{
	if(state == curState) return;
	if (curState != NULL)
	{
		stateLayer->removeChild(curState, true);
	}
	curState = state;
	stateLayer->addChild(curState);
}

void Framework::lastState()
{
	if (mLastState == "") return;
	changeState(mLastState.c_str());
}

void Framework::popup( const char* name, void* data, float offsetX, float offsetY)
{
	std::string str(name);
	Dialog* dialog = dialogManager->getDialog(str);
	dialog->setData(data);
	dialog->refresh();
	dialog->popup(offsetX, offsetY);
}

float Framework::getScreenWidth()
{
	return pDirector->getWinSize().width;
}

float Framework::getScreenHeight()
{
	return pDirector->getWinSize().height;
}

void Framework::setStateManager( StateManager* stateManager )
{
	this->stateManager = stateManager;
}

void Framework::setDialogManager( DialogManager* dialogManager )
{
	this->dialogManager = dialogManager;
}
